package com.salesmanager.web.tags;

import com.salesmanager.core.business.content.model.FileContentType;
import com.salesmanager.web.common.ImageSize;
import com.salesmanager.web.utils.ImageFilePathUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class CmsImageUrlTag extends TagSupport {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(CmsImageUrlTag.class);

	private String imageName;
	private String code;
    private String size;
    private String fileContentType;
    private String style;
	public int doStartTag() throws JspException {
		try {
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
            String imagePath = null;
            if (fileContentType.equals(FileContentType.AD.name())){
                imagePath = ImageFilePathUtils.buildAdImageFilePath(code, imageName, null, request);

                if (StringUtils.isBlank(size) && !StringUtils.isBlank(style)){
                    imagePath += style;
                }
            } else if (fileContentType.equals(FileContentType.MESSAGE_MANAGER.name())){
                imagePath = ImageFilePathUtils.buildMessageImageFilePath(code, imageName, null, request);

                if (StringUtils.isBlank(size) && !StringUtils.isBlank(style)){
                    imagePath += style;
                }
            }else if (fileContentType.equals(FileContentType.APP.name())){
                imagePath = ImageFilePathUtils.buildAppImageFilePath(code, imageName, request);

                if (StringUtils.isBlank(size) && !StringUtils.isBlank(style)){
                    imagePath += style;
                }
            }

            pageContext.getOut().print(imagePath);
		} catch (Exception ex) {
			LOGGER.error("Error while getting content url", ex);
		}
		return SKIP_BODY;
	}

	public int doEndTag() {
		return EVAL_PAGE;
	}

	public void setImageName(String imageName) {
		this.imageName = imageName;
	}

	public String getImageName() {
		return imageName;
	}

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }
}
